package fibTests;

import java.util.ArrayList;

public class FibRecDynamic implements IFib {
	private int iterations;
	
	public FibRecDynamic() {
		results = new ArrayList<Long>();
		iterations = 0;
	}
	
	@Override
	public int getLastIterationsCounter() {
		return iterations;
	}

	private ArrayList<Long> results;
	
	@Override
	public long fib(int n) {
		if (n < 0) return 0;
		results.clear();
		results.ensureCapacity(n);
		results.add((long)0);
		results.add((long)1);
		results.add((long)1);
		iterations = 1;
		if (results.size() > n) return results.get(n);
		return fibInner(n-1)+fibInner(n-2);
	}
	
	private long fibInner(int n) {
		iterations++;
		if (results.size() > n) return results.get(n);
		results.add(fibInner(n-1)+fibInner(n-2));
		return results.get(n);
	}

}
